package AccessDB;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import model.Bimestre;

public class BimestreDAO {
	
	Connection con; 
	
	public BimestreDAO(){
		con = ConnectionDB.getInstance().getConnection();
	}
	
	public List<Bimestre> retornaBimestresDoAno(int ano) throws SQLException{
		
		PreparedStatement stmt = con.prepareStatement("select * from Bimestre where ano = ?");
		stmt.setInt(1, ano);
		ResultSet rs = stmt.executeQuery();
		
		List<Bimestre> bimestres = new ArrayList<Bimestre>();
		
		while(rs.next())
			bimestres.add(gerarBimestre(rs));
	
		return bimestres;
	}
	
	private Bimestre gerarBimestre(ResultSet rs) throws SQLException{
		
		Bimestre bim = new Bimestre();
		
		bim.setAno(rs.getInt("ano"));
		bim.setIdBimestre(rs.getInt("idBimestre"));
		bim.setNumeroBimestre(rs.getInt("numBimestre"));
		bim.setFechado(rs.getBoolean("fechado"));
		
		return bim;
	}
	
}